<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-item label="客户名称">
              <a-input style="width:100%;" placeholder="请输入客户名称" v-model="queryParam.customerName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12" v-has="'XyParkCustomerList:all'">
            <a-form-item label="跟进人">
              <j-select-user-by-dep :multi="false"  v-model="queryParam.followUp"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="跟进状态">
              <a-input placeholder="请输入跟进状态" v-model="queryParam.customerStatus"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="12">
              <a-form-item label="客户来源">
                <j-dict-select-tag type="list" v-model="queryParam.customerSource" dictCode="xy_park_customer_source" placeholder="请选择客户来源"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="意向等级">
                <j-dict-select-tag type="list" v-model="queryParam.intentionLevel" dictCode="park_intention_level" placeholder="请选择意向等级"/>
              </a-form-item>
            </a-col>
          </template>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button v-has="'XyParkCustomerList:add'" @click="handleAdd" type="primary" icon="plus">新增</a-button> -->
      <!-- <a-button type="primary" icon="download" v-has="'XyParkCustomerList:export'" @click="handleExportXls('客户信息表')">导出</a-button>
      <a-upload v-has="'XyParkCustomerList:import'" name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="fieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <!-- <a-dropdown v-if="selectedRowKeys.length > 0" v-has="'XyParkCustomerList:batch_delete'">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleEdit(record)" v-has="'XyParkCustomerList:update'">编辑</a> -->
          <a v-has="'XyParkFollowUpList:add'" @click="handleFollowUp(record)" >客户跟进</a>
          <a-divider type="vertical" v-has="'XyParkFollowUpList:add'" />
          <a @click="handleFollowUpList(record)">跟进记录</a>
          <!-- <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleFollowUpList(record)">跟进记录</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-has="'XyParkCustomerList:delete'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown> -->
        </span>

      </a-table>
    </div>

    <xy-park-customer-modal ref="modalForm" @ok="modalFormOk"/>
    <XyParkFollowUpModal ref="XyParkFollowUpModal" @ok="() => this.loadData()" />
    <XyParkFollowUpListModal ref="XyParkFollowUpListModal" @ok="() => this.loadData()" />
  </a-card>
</template>

<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import XyParkCustomerModal from './modules/XyParkCustomerModal'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
import '@/assets/less/TableExpand.less'
import XyParkFollowUpModal from './modules/XyParkFollowUpModal'
import XyParkFollowUpListModal from './modules/XyParkFollowUpListModal'
import { filterObj, getLikeOrEqQueryParam } from '@/utils/util'
// import { USER_AUTH } from '@/store/mutation-types'
import { mapGetters } from 'vuex'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
// 高级查询modal需要参数
const superQueryFieldList = [
  { type: 'string', value: 'customerCode', text: '客户编码' },
  { type: 'string', value: 'customerName', text: '客户名称' },
  { type: 'select', value: 'customerTrade', text: '所属行业', dictCode: 'sex' },
  { type: 'select-user', value: 'followUp', text: '跟进人' },
  { type: 'string', value: 'customerStatus', text: '跟进状态' },
  { type: 'select', value: 'project', text: '来访项目', dictCode: 'xy_park_project,project_name,project_code' },
  { type: 'date', value: 'visitDate', text: '首次来访日期' },
  { type: 'select', value: 'customerSource', text: '客户来源', dictCode: 'xy_park_customer_source' },
  { type: 'select', value: 'intentionLevel', text: '意向等级', dictCode: 'park_intention_level' },
  { type: 'int', value: 'probability', text: '成交几率(%)' },
  { type: 'string', value: 'customerDemand', text: '客户需求', dictCode: 'park_customer_demand' },
  { type: 'select', value: 'renovation', text: '装修需求', dictCode: 'park_renovation' },
  { type: 'int', value: 'demandAreafrom', text: '需求面积自' },
  { type: 'int', value: 'demandAreato', text: '需求面积到' },
  { type: 'int', value: 'budgetFrom', text: '客户预算从(万)' },
  { type: 'int', value: 'budgetTo', text: '客户预算到(万)' }
]
export default {
  name: 'XyParkCustomerList',
  mixins: [JeecgListMixin],
  components: {
    XyParkCustomerModal,
    XyParkFollowUpModal,
    XyParkFollowUpListModal,
    JSelectUserByDep,
    JSuperQuery
  },
  data() {
    return {
      description: '客户信息表管理页面',
      queryParam: { bpmStatus: '2' },
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '客户编码',
          align: 'center',
          dataIndex: 'customerCode'
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: 'customerName'
        },
        {
          title: '所属行业',
          align: 'center',
          dataIndex: 'customerTrade_dictText'
        },
        {
          title: '跟进人',
          align: 'center',
          dataIndex: 'followUp_dictText'
        },
        {
          title: '跟进状态',
          align: 'center',
          dataIndex: 'customerStatus',
          customRender: function(text) {
            return filterDictTextByCache('customer_process', text)
          }
        },
        {
          title: '来访项目',
          align: 'center',
          dataIndex: 'project_dictText'
        },
        {
          title: '首次来访日期',
          align: 'center',
          dataIndex: 'visitDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '客户来源',
          align: 'center',
          dataIndex: 'customerSource_dictText'
        },
        {
          title: '意向等级',
          align: 'center',
          dataIndex: 'intentionLevel_dictText'
        },
        {
          title: '成交几率',
          align: 'center',
          dataIndex: 'probability'
        },
        {
          title: '客户需求',
          align: 'center',
          dataIndex: 'customerDemand_dictText'
        },
        {
          title: '装修需求',
          align: 'center',
          dataIndex: 'renovation_dictText'
        },
        {
          title: '需求面积自',
          align: 'center',
          dataIndex: 'demandAreafrom'
        },
        {
          title: '需求面积到',
          align: 'center',
          dataIndex: 'demandAreato'
        },
        {
          title: '客户预算从',
          align: 'center',
          dataIndex: 'budgetFrom'
        },
        {
          title: '客户预算到',
          align: 'center',
          dataIndex: 'budgetTo'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/community/xyParkCustomer/list',
        delete: '/community/xyParkCustomer/delete',
        deleteBatch: '/community/xyParkCustomer/deleteBatch',
        exportXlsUrl: '/community/xyParkCustomer/exportXls',
        importExcelUrl: 'community/xyParkCustomer/importExcel'

      },
      dictOptions: {},
      // isAll: false,
      disableMixinCreated: true,
      fieldList: superQueryFieldList
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    // const authList = JSON.parse(sessionStorage.getItem(USER_AUTH) || '[]')
    // authList.forEach(item => {
    //   if (item.action === 'XyParkCustomerList:all') {
    //     this.isAll = true
    //   }
    // })
    this.loadData()
  },
  methods: {
    ...mapGetters(['userInfo']),
    handleFollowUpList(data) {
      this.$refs.XyParkFollowUpListModal.show(data)
    },
    handleFollowUp(data) {
      this.$refs.XyParkFollowUpModal.add(data)
    },
    getQueryParams() {
      // 获取查询条件
      const sqp = {}
      if (this.superQueryParams) {
        sqp['superQueryParams'] = encodeURI(this.superQueryParams)
        sqp['superQueryMatchType'] = this.superQueryMatchType
      }
      const queryParam = getLikeOrEqQueryParam(this.queryParam)
      var param = Object.assign(sqp, queryParam, this.isorter, this.filters)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      // param.followUp = this.isAll ? param.followUp : this.userInfo().username
      return filterObj(param)
    },
    searchReset() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.queryParam = { bpmStatus: '2' }
      this.loadData(1)
    }
  }
}
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
